package com.hongdee.insurance.order.dao.impl;

import com.hongdee.base.basedao.impl.BaseDaoImpl;
import com.hongdee.insurance.order.dao.ITInsuranceOrderDao;
import com.hongdee.insurance.order.entity.TInsuranceOrder;
import com.hongdee.insurance.order.entity.TInsuranceOrderItem;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/4/27.
 */
@Service
public class TInsuranceOrderDaoImpl extends BaseDaoImpl implements ITInsuranceOrderDao {
    @Override
    public TInsuranceOrder info(String qid, String carId) throws Exception {
        String hql="from TInsuranceOrder where carId=:carId and quotationId=:quotationId";
        Map<String,Object> paramsMap=new HashMap<>();
        paramsMap.put("carId",carId);
        paramsMap.put("quotationId",qid);
        return (TInsuranceOrder) this.getHibernateDao().findEntityByHqlWithParams(hql,paramsMap);
    }

    @Override
    public TInsuranceOrder info(String id) throws Exception {
        String hql="from TInsuranceOrder where id=:id";
        Map<String,Object> paramsMap=new HashMap<>();
        paramsMap.put("id",id);
        return (TInsuranceOrder) this.getHibernateDao().findEntityByHqlWithParams(hql,paramsMap);
    }

    /**
     * 获得保险对应的交强或商业险信息
     * @param orderId
     * @param type
     * @param contractNO
     * @return
     * @throws Exception
     */
    @Override
    public TInsuranceOrderItem getOrderItemInfo(String orderId, String type, String contractNO) throws Exception {
        String hql="from TInsuranceOrderItem where orderId=:orderId and contractType=:contractType and contractNO=:contractNO";
        Map<String,Object> params=new HashMap<>();
        params.put("orderId",orderId);
        params.put("contractType",type);
        params.put("contractNO",contractNO);
        return (TInsuranceOrderItem) this.getHibernateDao().findEntityByHqlWithParams(hql,params);
    }

    /**
     * 根据类型查询保险对应的商业险 交强险信息
     * @param orderId
     * @param type
     * @return
     * @throws Exception
     */
    @Override
    public TInsuranceOrderItem getOrderItemInfo(String orderId, String type) throws Exception {
        String hql="from TInsuranceOrderItem where orderId=:orderId and contractType=:contractType";
        Map<String,Object> params=new HashMap<>();
        params.put("orderId",orderId);
        params.put("contractType",type);
        return (TInsuranceOrderItem) this.getHibernateDao().findEntityByHqlWithParams(hql,params);
    }

    /**
     * 修改合同号 查询是否有合同号重复
     * @param contractNo
     * @param itemId
     * @return false 允许继续添加 说明有重复合同号， true允许继续添加
     * @throws Exception
     */
    @Override
    public boolean checkOrderItemInfoByContractNo(String contractNo,String itemId) throws Exception {
        List<Map<String,Object>> list=this.list("select * from t_insurance_order_item where contract_no=? and id<>?",contractNo,itemId);
        if(list.size()>0){
            return false;
        }
        return true;
    }

    /**
     * 删除保险单下的交强或商业险信息
     * @param orderId
     * @param type
     * @throws Exception
     */
    @Override
    public void delOrderItem(String orderId, String type) throws Exception {
        String sql="delete from T_INSURANCE_ORDER_ITEM where order_id=? and contract_type=?";
        this.executeSQL(sql,orderId,type);
    }
}
